package Linklist;

/**
 * @Classname JZ25合并两个排序的链表
 * @Description TODO
 * @Date 2023/2/10 8:35
 * @Created by xjl
 */
public class JZ25合并两个排序的链表 {

    class ListNode {
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    // 使用的三指针来实现 就和两个数组的合并是一样的结果
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        }
        if (l2 == null) {
            return l1;
        }
        ListNode dumpy = new ListNode(-1);
        ListNode curr = dumpy;

        while (l1 != null && l2 != null) {
            if (l1.val <= l2.val) {
                curr.next = l1;
                l1 = l1.next;
                curr = curr.next;
            } else {
                curr.next = l2;
                l2 = l2.next;
                curr = curr.next;
            }
        }
        if (l1 == null) {
            curr.next = l2;
        }
        if (l2 == null) {
            curr.next = l1;
        }
        return dumpy.next;
    }

    public ListNode mergeTwoLists3(ListNode l1, ListNode l2) {
        ListNode dum = new ListNode(0);
        ListNode cur = dum;
        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                cur.next = l1;
                l1 = l1.next;
            } else {
                cur.next = l2;
                l2 = l2.next;
            }
            cur = cur.next;
        }
        // 三元运算符
        cur.next = l1 != null ? l1 : l2;
        return dum.next;
    }

    public ListNode mergeTwoLists4(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        } else if (l2 == null) {
            return l1;
        } else if (l1.val < l2.val) {
            // 循环的调用
            l1.next = mergeTwoLists4(l1.next, l2);
            return l1;
        } else {
            // 循环的调用
            l2.next = mergeTwoLists4(l1, l2.next);
            return l2;
        }
    }

    // 还可以利用变成一个链表 然后在使用单链表排序的原理来实现
    public ListNode mergeTwoLists2(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        }
        if (l2 == null) {
            return l1;
        }
        ListNode curr = l1;
        while (curr.next != null) {
            curr = curr.next;
        }
        curr.next = l2;
        return sortList(l1,null);
    }

    // 无序单链表排序
    public ListNode sortList(ListNode head,ListNode tail) {
        if (head == null||head.next==null) {
            return head;
        }
        // 快慢指针
        ListNode slow = head, fast = head;
        while (fast != tail) {
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode mid = slow;
        ListNode list1 = sortList(head, mid);
        ListNode list2 = sortList(mid, tail);
        ListNode sorted = merge(list1, list2);
        return sorted;
    }

    public ListNode merge(ListNode head1, ListNode head2) {
        ListNode dummyHead = new ListNode(0);
        ListNode temp = dummyHead, temp1 = head1, temp2 = head2;
        while (temp1 != null && temp2 != null) {
            if (temp1.val <= temp2.val) {
                temp.next = temp1;
                temp1 = temp1.next;
            } else {
                temp.next = temp2;
                temp2 = temp2.next;
            }
            temp = temp.next;
        }
        if (temp1 != null) {
            temp.next = temp1;
        } else if (temp2 != null) {
            temp.next = temp2;
        }
        return dummyHead.next;
    }
}
